//
//  DetailDynameTableViewCell.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/4/11.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "DetailDynameTableViewCell.h"

@implementation DetailDynameTableViewCell
@synthesize nameLabel;
@synthesize contentLabel;
@synthesize timeLabel;
@synthesize headImage;
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    //123
    // ignore the style argument and force the creation with style UITableViewCellStyleSubtitle
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    
    //初始化可变数组
    _imageArr = [NSMutableArray array];
    
    
    
    if (self) {
        
        //内容
        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, [UIScreen mainScreen].bounds.size.width-40, 0)];
        contentLabel.textColor = RGBACOLOR(85, 85, 85, 1);
        contentLabel.font = [UIFont systemFontOfSize:14];
        contentLabel.backgroundColor = [UIColor clearColor];
        contentLabel.numberOfLines = 0;
        [self.contentView addSubview:contentLabel];
    
    }
    return self;
}

-(CGRect)upDateCellWithDic:(NSDictionary *)dict{
    
    //内容
    self.contentLabel.text = [dict objectForKey:@"scontent"];
    
    //图片数组
    NSString * fileidsStr = [dict objectForKey:@"fileid"];
    NSArray *fileids;
    
    if (fileidsStr.length != 0) {
        
        fileids = [fileidsStr componentsSeparatedByString:@","];
    }
    
    
    [self.contentLabel setFrame:CGRectMake(self.contentLabel.frame.origin.x, self.contentLabel.frame.origin.y, self.contentLabel.frame.size.width, [self resizeContenLabelHeightWithSting:[dict objectForKey:@"scontent"]])];
    
    _images = nil;
    _images = fileids;
    
    for (UIView *subview in _bgView.subviews) {
        [subview removeFromSuperview];
    }
    
    _bgView = nil;
    [_imageArr removeAllObjects];
    
    if (fileids.count != 0) {  //有图片
        
        _bgView = [[UIView alloc] init];
        [self.contentView addSubview:_bgView];
        
        
        NSInteger line = 0;
        if ([fileids count]%3 == 0) {
            line = [fileids count]/3;
        }else
        {
            line = [fileids count]/3 + 1;
        }
        
        _bgView.frame = CGRectMake(0, CGRectGetMaxY(self.contentLabel.frame)+5, SCREENWIDTH,(12.5 + 75) * line - 12.5);
        
        _bgView.userInteractionEnabled = YES;
        
    }
    
    CGRect rect;
    
    for (NSInteger index = 0 ;index < [fileids count] ; index ++) {
        
        NSString *iamgename = [fileids objectAtIndex:index];
        rect = CGRectMake(25+ (75 + 12.5) * (index %3),(75 + 12.5) * (index / 3), 75, 75);
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
        
        imageView.userInteractionEnabled = YES;
        
        NSString * imageString = [NSString stringWithFormat:@"%@%@&s=%d",IMAGEHOSTURL,iamgename,ImageWidht];
        
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageString]];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        
        [imageView addGestureRecognizer:tap];
        imageView.tag = index;
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        [_imageArr addObject:imageView];
        
        [_bgView addSubview:imageView];
        
    }
    
    return rect;
}

-(void)tapAction:(UIGestureRecognizer *)gesture{
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = _bgView;
    browser.imageCount = _images.count;
    
    browser.currentImageIndex = (int)gesture.view.tag;
    browser.delegate = self;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _images[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    __block UIImage *_image;
    [imageView sd_setImageWithURL:[NSURL URLWithString:logoStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        _image = image;
    }];
    return _image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _images[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    
    NSURL *url = [NSURL URLWithString:logoStr];
    return url;
}


#pragma mark -- other
- (CGFloat)resizeContenLabelHeightWithSting:(NSString *)text
{
    if (text.length == 0) {
        return 0;
    }
    UIFont *font = [UIFont systemFontOfSize:14];
    
    NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin;
    
    //NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    //[style setLineBreakMode:NSLineBreakByCharWrapping];
    
    NSDictionary *attributes = @{ NSFontAttributeName : font};
    
    CGSize maxSize = CGSizeMake(SCREENWIDTH - 15 - 55, 600);
    CGSize size = [text boundingRectWithSize:maxSize
                                     options:opts
                                  attributes:attributes
                                     context:nil].size;
    return size.height;
}


@end
